﻿@model PollModel

<div class="panel-group">
    @await Component.InvokeAsync("AdminWidget", new { widgetZone = "admin_poll_details_answers_top", additionalData = Model.Id })
    <div class="panel panel-default">
        @if (Model.Id > 0)
        {
            <div class="panel-body">
                <div id="answers-grid"></div>

                <script type="text/javascript">
                    $(document).ready(function() {
                        $("#answers-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("PollAnswers", "Poll", new {pollId = Model.Id}))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    },
                                    create: {
                                        url: "@Html.Raw(Url.Action("PollAnswerAdd", "Poll", new {pollId = Model.Id}))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    },
                                    update: {
                                        url: "@Html.Raw(Url.Action("PollAnswerUpdate", "Poll"))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    },
                                    destroy: {
                                        url: "@Html.Raw(Url.Action("PollAnswerDelete", "Poll"))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors",
                                    model: {
                                        id: "Id",
                                        fields: {
                                            Name: { editable: true, type: "string" },
                                            NumberOfVotes: { editable: false, type: "number" },
                                            DisplayOrder: { editable: true, type: "number" },
                                            Id: { editable: false, type: "number" }
                                        }
                                    }
                                },
                                requestEnd: function(e) {
                                    if (e.type == "create" || e.type == "update") {
                                        this.read();
                                    }
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                numeric: false,
                                previousNext: false,
                                info: false,
                                @await Html.PartialAsync("_GridPagerMessages")
                            },
                            toolbar: [{ name: "create", text: "@T("Admin.Common.AddNewRecord")" }],
                            editable: {
                                confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [
                                {
                                    field: "Name",
                                    title: "@T("Admin.ContentManagement.Polls.Answers.Fields.Name")",
                                    width: 300
                                }, {
                                    field: "NumberOfVotes",
                                    title: "@T("Admin.ContentManagement.Polls.Answers.Fields.NumberOfVotes")",
                                    width: 100
                                }, {
                                    field: "DisplayOrder",
                                    title: "@T("Admin.ContentManagement.Polls.Answers.Fields.DisplayOrder")",
                                    //integer format
                                    format: "{0:0}",
                                    width: 100
                                }, {
                                    command: [
                                        {
                                            name: "edit",
                                            text: {
                                                edit: "@T("Admin.Common.Edit")",
                                                update: "@T("Admin.Common.Update")",
                                                cancel: "@T("Admin.Common.Cancel")"
                                            }
                                        }, {
                                            name: "destroy",
                                            text: "@T("Admin.Common.Delete")"
                                        }
                                    ],
                                    width: 200
                                }
                            ]
                        });
                    });

                </script>
            </div>
        }
        else
        {
            <div class="panel-body">
                @T("Admin.ContentManagement.Polls.Answers.SaveBeforeEdit")
            </div>
        }
    </div>
    @await Component.InvokeAsync("AdminWidget", new { widgetZone = "admin_poll_details_answers_bottom", additionalData = Model.Id })
</div>